﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;


public partial class UpZBZL : System.Web.UI.Page
{    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userName"] == null || Session["roleID"] == null)
        {
            Response.Redirect("Login.aspx");
            Response.End();
        }
        else if (Session["roleID"].ToString() != "1")
        {            
            Response.Write("<script language='javascript'>alert('对不起！你无权提交指标资料！');location.href='MessageInfo.aspx'</script>");            
        }
        if (!Page.IsPostBack)
        {
            lblUserName.Text = BasicClass.GetTrueNameByUserName(Session["userName"].ToString());
            FileUploadDaTa.Attributes.Add("onchange", "getfilename(this.value)");
            txtWriteDate.Value = DateTime.Now.ToShortDateString();

        }
        
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (UpMainFile())
        {
            string WriteDateString = txtWriteDate.Value.Trim();
            DateTime WriteDate = Convert.ToDateTime(WriteDateString);
            string FilesPath = txtUpFileStatus.Text;
            ZBZLOperator data = new ZBZLOperator();
            data.Title = BasicClass.HtmlEncode(txtTitle.Text);
            data.SubTitle = txtSubTitle.Text;
            data.UserName = Session["userName"].ToString();
            data.WriteDate = WriteDate;
            data.FilesPath = FilesPath;
            data.Brief = BasicClass.HtmlEncode(txtDataBrief.Text);
            
            if (data.AddZBZL())
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('指标资料上传成功!');location.href='UpZBZL.aspx';", true);

            }
        }

    }
    protected void txtReset_Click(object sender, EventArgs e)
    {

    }
    protected void btnUpFile_Click(object sender, EventArgs e)
    {

    }
    private string UpDataFile()
    {
        string fullFileName = FileUploadDaTa.PostedFile.FileName.ToString().Trim();
        string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1).ToLower();
        string SaveFilename = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);
        if (type == "exe")
        //if (type.ToString() != "jpg" && type != "jpeg" && type != "bmp" && type != "gif" && type != "png")
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('文件格式不对！');", true);
        }
        else
        {
            DataOperation DBO = new DataOperation();            
            string fileDir = "ZBZL";
            string FilePath = DBO.CheckDirectory(fileDir);
            string DataFilePath = FilePath + "\\" + SaveFilename;
            if (File.Exists(Server.MapPath("files") + DataFilePath))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('主文件重名，请重命名主文件后再上传！');", true);
                return null;
            }
            else
            {
                FileUploadDaTa.PostedFile.SaveAs(Server.MapPath("files") + DataFilePath);
                return DataFilePath;
            }

        }
        return null;
    }
    protected Boolean UpMainFile()
    {
        bool success = false;
        if (CheckFiles())
        {
            string UpFilePath = FileUploadDaTa.PostedFile.FileName.ToString().Trim();
            if (UpFilePath != "")
            {
                try
                {
                    string filePath = UpDataFile();
                    if (filePath != null)
                    {
                        string FileName = UpFilePath.Substring(UpFilePath.LastIndexOf("\\") + 1);
                        string FileNameShort = FileName.Substring(0, FileName.LastIndexOf("."));
                        txtUpFileStatus.Text = filePath;
                        txtSubTitle.Text = FileName;
                        success = true;
                    }
                    else
                    {
                        success = false;
                    }

                }
                catch
                {
                    success = false;
                }
            }
        }
        return success;
    }
    protected Boolean CheckFiles()
    {
        lblFailMsg.Text = "<br/>";
        DataOperation DBO = new DataOperation();        
        string fileDir = "ZBZL";
        string FilePath = DBO.CheckDirectory(fileDir);
        bool fileCheck = true;
        string errorMsg = "以下文件服务器中存在同名文件：";
        for (int j = 0; j < Request.Files.Count; j++)
        {
            string filePath = Request.Files[j].FileName;
            //判断上传文件框里是否有文件
            if (filePath != "")
            {
                string shortFileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
                string DataFilePath = FilePath + "\\" + shortFileName;
                if (File.Exists(Server.MapPath("files") + DataFilePath))
                {
                    lblFailMsg.Text = lblFailMsg.Text + "<font color='red'>存在同名文件：" + shortFileName + "</font>;请更名后再添加该文件。<br/>";
                    errorMsg += shortFileName + ",";
                    fileCheck = false;
                }
            }
        }
        if (!fileCheck)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('" + errorMsg + "');", true);
        }
        return fileCheck;
    }
}
